#!/bin/bash
if [[ $(whoami) != "lfs" ]]; then
	echo "Please log in as user lfs...";
	exit 1;
fi
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
#######################################
# set package name here
PACKAGE=$(echo $0 | sed -e 's/.*make_\(.*\).sh/\1/');
#######################################
PKG_NAME=$(pkgname extra $PACKAGE); if (($? != 0)); then exit 1; fi;
PKG_VERSION=$(pkgversion extra $PACKAGE); if (($? != 0)); then exit 2; fi;
PKG_ARCHIVE=$(pkgarchive extra $PACKAGE); if (($? != 0)); then exit 3; fi;
echo "make $PACKAGE ($PKG_NAME) with version $PKG_VERSION from archive $PKG_ARCHIVE"
PKG_FOLDER=$(unpack $PKG_ARCHIVE); if (($? != 0)); then exit 4; fi;
PKG_DEST=/usr/pkg/$PKG_NAME/$PKG_VERSION;
cd ../$PKG_FOLDER || exit 5;
#######################################
# prepare package source (patch etc.)
sed -i 's#@CUPS_HTMLVIEW@#firefox#' desktop/cups.desktop.in


#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package preparing with code $ERR";
    exit 6;
fi
#######################################
# configure and make package
sed -i 's:555:755:g;s:444:644:g' Makedefs.in                                  &&
sed -i '/MAN.EXT/s:.gz::g' configure config-scripts/cups-manpages.m4          &&
sed -i '/LIBGCRYPTCONFIG/d' config-scripts/cups-ssl.m4                        &&
sed -i 's@else /\* HAVE_AVAHI \*/@elif defined(HAVE_AVAHI)@' test/ippserver.c &&

aclocal  -I config-scripts &&
autoconf -I config-scripts &&

./configure --libdir=/usr/lib            \
            --disable-systemd            \
            --with-rcdir=/tmp/cupsinit   \
            --with-system-groups=lpadmin \
            --with-docdir=/usr/share/cups/doc-$PKG_VERSION &&
    make && \
    rm -vRf $PKG_DEST/inst && \
    mkdir -pv $PKG_DEST/inst/{usr/{bin,lib,include,share/doc},tmp,etc/pam.d} && \
    make DESTDIR=$PKG_DEST/inst install




#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 6;
fi
#######################################
# make post checks and configuration

rm -rf $PKG_DEST/inst/tmp/cupsinit &&
ln -svnf ../cups/doc-$PKG_VERSION $PKG_DEST/inst/usr/share/doc/cups-$PKG_VERSION &&
echo "ServerName /var/run/cups/cups.sock" > $PKG_DEST/inst/etc/cups/client.conf &&
rm -rf $PKG_DEST/inst/usr/share/cups/banners &&
rm -rf $PKG_DEST/inst/usr/share/cups/data/testprint &&

echo "# Begin /etc/pam.d/cups

auth    include system-auth
account include system-account
session include system-session

# End /etc/pam.d/cups" > $PKG_DEST/inst/etc/pam.d/cups


#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 7;
fi
echo "# Package information file... generated at make process" > $PKG_DEST/pkg.info && \
echo "# pkg name   source archive    version" >> $PKG_DEST/pkg.info && \
echo "$PKG_NAME  $PKG_ARCHIVE  $PKG_VERSION" >> $PKG_DEST/pkg.info && \
if [ -d $LFS/sources/pkg/extra/$PKG_NAME ]; then cp -aRv $LFS/sources/pkg/extra/$PKG_NAME/* $PKG_DEST; fi && \
cp -avf $LFS/sources/pkg/pkgutils.sh $PKG_DEST/pkgutils.sh && \
tar -czvvf /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz -C /usr/pkg/$PKG_NAME $PKG_VERSION && \
rm -Rfv /usr/pkg/$PKG_NAME && \
cd $LFS/sources && \
rm -Rfv $PKG_FOLDER || exit 8;
errcho "Build complete extra/$PACKAGE"
errcho "Package files are stored to: /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz"
echo " --OK--"
